home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / blowfish / asm / bf586.pl next >
Text File  |  2000-05-18  |  3KB  |  160 lines

  1.  
  2. #!/usr/local/bin/perl
  3.  
  4. $prog="bf586.pl";
  5.  
  6. # base code is in microsft
  7. # op dest, source
  8. # format.
  9. #
  10.  
  11. if (    ($ARGV[0] eq "elf"))
  12.     { require "x86unix.pl"; }
  13. elsif (    ($ARGV[0] eq "a.out"))
  14.     { $aout=1; require "x86unix.pl"; }
  15. elsif (    ($ARGV[0] eq "sol"))
  16.     { $sol=1; require "x86unix.pl"; }
  17. elsif (    ($ARGV[0] eq "cpp"))
  18.     { $cpp=1; require "x86unix.pl"; }
  19. elsif (    ($ARGV[0] eq "win32"))
  20.     { require "x86ms.pl"; }
  21. else
  22.     {
  23.     print STDERR <<"EOF";
  24. Pick one target type from
  25.     elf    - linux, FreeBSD etc
  26.     a.out    - old linux
  27.     sol    - x86 solaris
  28.     cpp    - format so x86unix.cpp can be used
  29.     win32    - Windows 95/Windows NT
  30. EOF
  31.     exit(1);
  32.     }
  33.  
  34. &comment("Don't even think of reading this code");
  35. &comment("It was automatically generated by $prog");
  36. &comment("Which is a perl program used to generate the x86 assember for");
  37. &comment("any of elf, a.out, Win32, or Solaris");
  38. &comment("It can be found in SSLeay 0.7.0+");
  39. &comment("eric <eay\@mincom.oz.au>");
  40. &comment("");
  41.  
  42. &file("bfx86xxxx");
  43.  
  44. $BF_ROUNDS=16;
  45. $BF_OFF=($BF_ROUNDS+2)*4;
  46. $L="ecx";
  47. $R="edx";
  48. $P="edi";
  49. $tot="esi";
  50. $tmp1="eax";
  51. $tmp2="ebx";
  52. $tmp3="ebp";
  53.  
  54. &des_encrypt("BF_encrypt");
  55.  
  56. &file_end();
  57.  
  58. sub des_encrypt
  59.     {
  60.     local($name)=@_;
  61.  
  62.     &function_begin($name,3);
  63.  
  64.     &comment("");
  65.     &comment("Load the 2 words");
  66.     &mov("eax",&wparam(0));
  67.     &mov($L,&DWP(0,"eax","",0));
  68.     &mov($R,&DWP(4,"eax","",0));
  69.  
  70.     &comment("");
  71.     &comment("P pointer, s and enc flag");
  72.     &mov($P,&wparam(1));
  73.  
  74.     &xor(    $tmp1,    $tmp1);
  75.     &xor(    $tmp2,    $tmp2);
  76.  
  77.     # encrypting part
  78.  
  79.     &mov("ebp",&wparam(2));    # get encrypt flag
  80.     &cmp("ebp","0");
  81.     &je(&label("start_decrypt"));
  82.  
  83.     &xor($L,&DWP(0,$P,"",0));
  84.     for ($i=0; $i<$BF_ROUNDS; $i+=2)
  85.         {
  86.         &comment("");
  87.         &comment("Round $i");
  88.         &BF_ENCRYPT($i+1,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
  89.  
  90.         &comment("");
  91.         &comment("Round ".sprintf("%d",$i+1));
  92.         &BF_ENCRYPT($i+2,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
  93.         }
  94.     &xor($R,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
  95.  
  96.     &mov("eax",&wparam(0));
  97.     &mov(&DWP(0,"eax","",0),$R);
  98.     &mov(&DWP(4,"eax","",0),$L);
  99.     &function_end_A($name);
  100.  
  101.     &set_label("start_decrypt");
  102.  
  103.     &xor($L,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
  104.     for ($i=$BF_ROUNDS; $i>0; $i-=2)
  105.         {
  106.         &comment("");
  107.         &comment("Round $i");
  108.         &BF_ENCRYPT($i,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
  109.         &comment("");
  110.         &comment("Round ".sprintf("%d",$i-1));
  111.         &BF_ENCRYPT($i-1,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
  112.         }
  113.     &xor($R,&DWP(0,$P,"",0));
  114.  
  115.     &mov("eax",&wparam(0));
  116.     &mov(&DWP(0,"eax","",0),$R);
  117.     &mov(&DWP(4,"eax","",0),$L);
  118.     &function_end_A($name);
  119.  
  120.     &function_end_B($name);
  121.     }
  122.  
  123. sub BF_ENCRYPT
  124.     {
  125.     local($i,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3)=@_;
  126.  
  127.     &rotr(    $R,        16);
  128.     &mov(    $tot,        &DWP(&n2a($i*4),$P,"",0));
  129.  
  130.     &movb(    &LB($tmp1),    &HB($R));
  131.     &movb(    &LB($tmp2),    &LB($R));
  132.  
  133.     &rotr(    $R,        16);
  134.     &xor(    $L,        $tot);
  135.  
  136.     &mov(    $tot,        &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
  137.     &mov(    $tmp3,        &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
  138.  
  139.     &movb(    &LB($tmp1),    &HB($R));
  140.     &movb(    &LB($tmp2),    &LB($R));
  141.  
  142.     &add(    $tot,        $tmp3);
  143.     &mov(    $tmp1,        &DWP(&n2a($BF_OFF+0x0800),$P,$tmp1,4)); # delay
  144.  
  145.     &xor(    $tot,        $tmp1);
  146.     &mov(    $tmp3,        &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp2,4));
  147.  
  148.     &add(    $tot,        $tmp3);
  149.     &xor(    $tmp1,        $tmp1);
  150.  
  151.     &xor(    $L,        $tot);                    
  152.     # delay
  153.     }
  154.  
  155. sub n2a
  156.     {
  157.     sprintf("%d",$_[0]);
  158.     }
  159.  
  160.